package com.study.mike.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.study.mike.mapper.UserRoleMapper;
import com.study.mike.model.dto.HuToolDTO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.*;


/**
 * @author kh
 */
@SpringBootTest
public class HuTool {
    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 时间格式化工具
     */
    @Test
    void test() {
        Date date = new Date();
        System.out.println(date);
        System.out.println(DateUtil.date());
        System.out.println(DateUtil.date(System.currentTimeMillis()));

        // 字符串转化为日期
        String dateStr = "2017-03-01";
        Date date1 = DateUtil.parse(dateStr, "yyyy-MM-dd");
        System.out.println(date1);

        //日期转化为字符串
        String dateStr1 = "2017-03-01";
        Date date2 = DateUtil.parse(dateStr1);
        String format = DateUtil.format(date2, "yyyy/MM/dd");


        System.out.println(DateUtil.isLeapYear(2017));


    }

    /**
     * 字符串工具
     */
    @Test
    void test2() {
        String str = "hello";
//        System.out.println(StrUtil.hasBlank(str));
//        String str1 = "pretty_girl.jpg";
//        // 去掉前缀部分
//        System.out.println(StrUtil.removeSuffix(str1, ".jpg"));
//
//        // 去掉后缀
//        System.out.println(StrUtil.removePrefix(str1, "p"));
        String str1 = "J000295001";
        System.out.println(StrUtil.removePrefix(str1, "J000"));
        System.out.println(str1);

    }

    /**
     * 文件工具类
     */
    @Test
    void test3() {
        // 创建文件
//      FileUtil.touch("C:\\Users\\James\\Desktop\\test.txt");

        // 删除文件或目录（递归删除，不判断是否为空）
//        FileUtil.del("C:\\Users\\James\\Desktop\\test.txt");
//
//        System.out.println(FileUtil.ls("C:\\Users\\James\\Desktop").length);

        // 文件类型
        File file = FileUtil.file("C:\\Users\\James\\Desktop\\test.txt");
        String type = FileTypeUtil.getType(file);
        //输出 文件类型
        Console.log(type);

        // 更改文件后缀名
        FileTypeUtil.putFileType("C:\\Users\\James\\Desktop\\pi.png", "jpg");
    }


    @Test
    void test4() {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        list1.add("hh");
        list.addAll(list1);
        System.out.println(list);


//        Map<String,Integer> map = new HashMap<String,Integer>();
//        public void add(String key,Integer value){
//            if(map.get(key)!=null){
//            map.put(key,map.get(key)+value); }
//            else{ map.put(key,value);
//        }
//        }

    }

    @Test
    void testDeSensitizedUtil() {
        // 地址
        System.out.println(DesensitizedUtil.address("重庆市渝北区竹林路慈竹苑32栋4单元1-4", 8));

        // 银行卡
        System.out.println(DesensitizedUtil.bankCard("6214651043063709"));

        // 中文名
        System.out.println(DesensitizedUtil.chineseName("周建设"));

        // 邮箱
        System.out.println(DesensitizedUtil.email("452481889@qq.com"));

        // 手机号（fixPhone）
        System.out.println(DesensitizedUtil.fixedPhone("17783791782"));

        // 手机号（mobilePhone）
        System.out.println(DesensitizedUtil.mobilePhone("17783791782"));

        // 身份证号码 front，前面留取位数、end，后面留取位数
        System.out.println(DesensitizedUtil.idCardNum("500238199910162711", 4, 4));

        // 密码
        System.out.println(DesensitizedUtil.password("smainnegchonbglai"));

    }

    @Test
    void testTreeUtil() {
        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        nodeList.add(new TreeNode<>("1", "0", "系统管理", 10));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 8));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 5));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 10));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 8));
        nodeList.add(new TreeNode<>("221", "2", "类别管理", 8));

        // 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("id");
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        //转换器

        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
//                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("extraField", 666);
                    tree.putExtra("other", new Object());
                });

        System.out.println(treeNodes);

    }

    @Test
    public void testDateTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        HuToolDTO huToolDTO = new HuToolDTO();
        huToolDTO.setCreateTime(new DateTime());
        huToolDTO.setUpdateTime(new DateTime());
        huToolDTO.setId(12);
        System.out.println(huToolDTO);
    }

    @Test
    public void testXiGua() {
        Map<String, String> cat = new HashMap<>();
        cat.put("name", "西瓜");
        cat.put("age", "four month");
        cat.put("type", "Gold gradient");
        cat.put("gender", "male");
        for (Map.Entry<String, String> entry : cat.entrySet()) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }
    }

    /**
     * 非HuTool
     */
    @Test
    public void testMyTreeUtil(){
//        GetUserResourcePO getUserResourcePO = new GetUserResourcePO();
//        getUserResourcePO.setUserId(1);
//        List<UserResourceDTO> userResourceDTOS = userResourceMapper.treeInfo(getUserResourcePO);
//        Map<Long,List<UserResourceDTO>> map = userResourceDTOS.stream().filter(item->item.getParentId()!=0).collect(Collectors.groupingBy(UserResourceDTO::getParentId));
//        userResourceDTOS.forEach(item-> item.setChildren(map.get(item.getId())));
//        List<UserResourceDTO> dtoList =  userResourceDTOS.stream().filter(node -> node.getParentId() == 0).collect(Collectors.toList());
//        System.out.println(dtoList);

    }



}

